package ru.kruzhki.client.ui.addcompany;

import java.util.Arrays;

import ru.kruzhki.client.gwtp.AbstractView;
import ru.kruzhki.client.ui.addcompany.AddCompanyPresenter.MyViewHandler;
import ru.kruzhki.shared.model.Company;
import ru.kruzhki.shared.model.District;

import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
import com.sencha.gxt.data.shared.LabelProvider;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.SimpleComboBox;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;

public class AddCompanyView extends AbstractView<ContentPanel> implements AddCompanyPresenter.MyView {
	private TextField companyName;
    private SimpleComboBox<District> district;
    private TextField address;
    private TextField phone;
    private TextField url;
    private TextArea annotation;
    private MyViewHandler viewHandler;

    @Override
	protected ContentPanel createWidget() {
        ContentPanel widget = new ContentPanel();
        widget.setHeaderVisible(false);
        widget.setBorders(true);
        widget.getBody().setBorders(false);

        
        widget.setBodyStyle("background: none; padding: 15px");
        widget.setLayoutData(new VerticalLayoutData(1, 1));
     
        VerticalLayoutContainer p = new VerticalLayoutContainer();
        widget.add(p);
     
        companyName = new TextField();
        p.add(new FieldLabel(companyName, "Название компании"), new VerticalLayoutData(1, -1));
        district = new SimpleComboBox<District>(new LabelProvider<District>() {

            @Override
            public String getLabel(District item) {
                return item.getTitle();
            }
        });
        district.add(Arrays.asList(District.values()));
        district.setForceSelection(false);
        district.setEditable(false);
        district.setTriggerAction(TriggerAction.ALL);
        p.add(new FieldLabel(district, "Район"), new VerticalLayoutData(1, -1));

        address = new TextField();
        p.add(new FieldLabel(address, "Адрес"), new VerticalLayoutData(1, -1));

        phone = new TextField();
        p.add(new FieldLabel(phone, "Телефон"), new VerticalLayoutData(1, -1));


        url = new TextField();
        p.add(new FieldLabel(url, "WEB-сайт"), new VerticalLayoutData(1, -1));

        annotation = new TextArea();
        annotation.setHeight(100);
        p.add(new FieldLabel(annotation, "Примечание"), new VerticalLayoutData(1, -1));

        TextButton addCompanyButton = new TextButton("Добавить компанию");
        addCompanyButton.addSelectHandler(new SelectHandler() {
            @Override
            public void onSelect(SelectEvent event) {
                Company company = new Company();
                company.setTitle(companyName.getCurrentValue());
                company.setDistrict(district.getCurrentValue());
                company.setAddress(address.getCurrentValue());
                company.setUrl(url.getCurrentValue());
                company.setAnnotation(annotation.getCurrentValue());
                viewHandler.onAddCompany(company);
            }
        });
        widget.addButton(addCompanyButton);
     
        return widget;
        
	}

    @Override
    public void setViewHandler(MyViewHandler viewHandler) {
        this.viewHandler=viewHandler;
    }
	
}
